/**
 * 网络响应工具类
 */
//= ==========

export interface ResponseDataType<T = any> {
  code: '200' | string;
  status: number;
  data: T;
  message: 'ok' | string;
}

/**
 * 响应成功
 * @param data 返回结果
 */
export function BeSuccess<T>(data?: any): ResponseDataType<T> {
  return {
    code: '200',
    status: 1,
    data: data === undefined ? 'ok' : data,
    message: 'ok',
  };
}
/**
 * 响应错误
 * @param msg 错误消息
 * @param code 错误代码
 */
export function BeError(error: any, code = 0, data?: any): ResponseDataType {
  if (typeof error === 'string') {
    return {
      code: `${code}`,
      status: code,
      message: error,
      data,
    };
  }
  return {
    code: `${code}`,
    status: code,
    message: (error as Error).message,
    data,
  };
}
